<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>

        console.log(0.1 + .3 === 0.3)//false
        console.log(0.1.toString(2)) //转换为二进制 会进行四舍五入0.0001100110011001100110011001100110011001100110011001101 
        console.log(.2.toString(2))//0.001100110011001100110011001100110011001100110011001101
        //两者都是无限不循环小数  则这么两个数想加会存在不相等


        // 解决方法
        //找出小数最长的 长度1=>10  长度2=>100 长度 3=>1000  依次类推 都进行乘法再进行除法
        let a = .1, b = .2;
        let aa = String(a), bb = String(b)
        let len = aa.split('.')[1].length;
        let len2 = bb.split('.')[1].length
        let maxlen = len > len2 ? len : len2
        let integer = 1
        for (let i = 0; i < maxlen; i++) {
            integer = integer * 10
        }
        console.log(a + b === 0.3, (a * integer + b * integer) / integer === .3)//false  true


        /*
        js最大安全数 2*53    -2*53  超过范围后数值将不准确
        js能表示的 2**1024-1   -2**1024
        */

    </script>

</body>

</html>